package com.ruoyi.web.controller.other;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.street.domain.HouseStreetT;
import com.ruoyi.street.service.IHouseStreetTService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 街道办事处Controller
 * 
 * @author tuyeqi
 */
@RestController
@RequestMapping("/other/street")
public class HouseStreetTController extends BaseController
{
    private final IHouseStreetTService houseStreetTService;
    @Autowired
    private ISysUserService userService;

    public HouseStreetTController(IHouseStreetTService houseStreetTService) {
        this.houseStreetTService = houseStreetTService;
    }

    /**
     * 查询街道办事处列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HouseStreetT houseStreetT)
    {
        startPage();
        List<HouseStreetT> list = houseStreetTService.selectHouseStreetTList(houseStreetT);
        return getDataTable(list);
    }

    /**
     * 导出街道办事处列表
     */
    @Log(title = "街道办事处", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HouseStreetT houseStreetT)
    {
        List<HouseStreetT> list = houseStreetTService.selectHouseStreetTList(houseStreetT);
        ExcelUtil<HouseStreetT> util = new ExcelUtil<>(HouseStreetT.class);
        util.exportExcel(response, list, "街道办事处数据");
    }

    /**
     * 获取街道办事处详细信息
     */
    @GetMapping(value = "/{streetId}")
    public AjaxResult getInfo(@PathVariable("streetId") Long streetId)
    {
        return AjaxResult.success(houseStreetTService.selectHouseStreetTByStreetId(streetId));
    }

    /**
     * 新增街道办事处
     */
    @Log(title = "街道办事处", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseStreetT houseStreetT)
    {
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(houseStreetT.getUserName())))
        {
            return AjaxResult.error("新增用户'" + houseStreetT.getUserName() + "'失败，登录账号已存在");
        }
        HouseStreetT vo=new HouseStreetT();
        SysUser user=new SysUser();
        user.setCreateBy(getUsername());
        user.setPassword(SecurityUtils.encryptPassword(houseStreetT.getPassword()));
        user.setUserName(houseStreetT.getStreetName());
        user.setNickName(houseStreetT.getStreetName());
        user.setRoleId(102L);
        userService.insertUser(user);
        houseStreetT.setCreater(getUsername());
        return toAjax(houseStreetTService.insertHouseStreetT(houseStreetT));
    }

    /**
     * 修改街道办事处
     */
    @Log(title = "街道办事处", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseStreetT houseStreetT)
    {
        return toAjax(houseStreetTService.updateHouseStreetT(houseStreetT));
    }

    /**
     * 删除街道办事处
     */
    @Log(title = "街道办事处", businessType = BusinessType.DELETE)
	@DeleteMapping("/{streetIds}")
    public AjaxResult remove(@PathVariable Long[] streetIds)
    {

        return toAjax(houseStreetTService.deleteHouseStreetTByStreetIds(streetIds));
    }
}
